<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace c\goods;
\lang::load('shop');

class comment extends \control
{
    var $table = 'goods_comment';
	var $class = array('PU'=>'\c\auth\permission');
	var $Val = array(1,0,-1);
    
	use \m\comment;
	
	function gVal()
	{
		return \lang::Ag_p($this->Val, 'comment_val');
	}
	
	function index_post()
    {
		$this->PU->login();
		$id = intval($_GET['data_id']);
		$ord = $this->db->getOne('shop_order', 'user_id', 'id='. $id);
		if ($ord['user_id']!=\YF_f::$login_user_id)
			$this->isExist(FALSE);
		
		$res = $this->db->get('shop_detail', '*', 'order_id='. $id);
		$this->loadTemplate('goods/comment/post', array('res'=>$res));
    }
	function index_save()
    {
		$this->PU->login();
		$ord_id = intval($this->PU->u_de($_POST['ord_id']));
		$ord = $this->db->getOne('shop_order', 'state', 'id='. $ord_id);
		if ($ord['state']!=5)
			ajax_json(\lang::g('Over'));
		
		$_id = $_POST['data_id'];
		$_content = $_POST['content'];
		$_val = $_POST['val'];
		if (!is_array($_id) || count($_id)==0)
			ajax_json(\lang::g('Over'));
		
		//name
		$n = \YF_f::$arr['login_user']['name'];
		$l = mb_strlen($n);
		$n1 = mb_substr($n, 0, 1);
		if ($l<4)
			$n1 .= str_repeat('*', $l-1);
		else{
			$n1 .= str_repeat('*', $l-2);
			$n1 .= mb_substr ($n, -1, 1);
		 }
		$user_name = $n1;
		unset($n, $l, $n1);
		
		$this->db->sqlS();
		foreach($_id as $k=>$v)
		 {
			$d = array(
				'data_id' => $this->PU->u_de($v),
				'val'=> $_val[$k],
				'content'=> $_content[$k],
				'user_name'=> $user_name,
				'parent_id'=> 0
			);
			$b = $this->save($d);
			if (!$b)
			 {
				$this->db->sqlB();
				ajax_json(\lang::g('save fail'));
			 }
		 }
		$this->db->sqlE();

		\YF_f::initC('\c\shop\order\g')->ok($ord_id);
		ajax_json(TRUE);
    }
	
	function li($data_id)
	{
		return $this->db->get_page(array(), $this->table, '*', 'data_id='. intval($data_id), 'id DESC');
	}  
}
